var express = require('express');
var router = express.Router();
var pool = require('../db/sql.js');
const { query } = require('express');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

// eslint-disable-next-line
router.get('/getUser', function(req, res, next) {
  let sql = "select * from user where userName = ?"
  let params = req.query.userName
  pool.query(sql, params, function(error, results, field) {
    res.json(results)
  })
})

router.post('/updataUserData', (req, res) => {
  let sql = "update user set userPhoto = ?, userEmail = ?, userIntroduce = ? where userName = ?"
  let params = [req.body.userPhoto, req.body.userEmail, req.body.userIntroduce ,req.body.userName]
  pool.query(sql, params, (error, results, field) => {
    if (error) throw error
  })
})

router.get('/getLeaveMsgList', function(req, res, next) {
  pool.query("select * from leavemsg", function(error, results, field) {
    res.json(results)
  })
})

router.post('/addLeaveMsg', (req, res) => {
  let sql = "insert into leavemsg (user, text, time) values (?, ?, ?)"
  let params = [req.body.user, req.body.text, req.body.time]
  pool.query(sql, params, (err, results, field) => {
    // if (err) throw err
  })
  // pool.end()
})

router.post('/regist', (req, res) => {
  let sql = "insert into user (userName, userPwd) value (?, ?)"
  let params = [req.body.userName, req.body.userPwd]
  pool.query(sql, params, (error, results, field) => {
    res.send({allowRegist: true})
  })
})

router.post('/login', (req, res) => {
  let sql = "select * from user where userName = ?"
  let params = [req.body.userName]
  const userPwd = req.body.userPwd
  pool.query(sql, params, (error, results, field) => {
    if (userPwd == results[0].userPwd) {
      console.log('密码正确')
      res.send({allowLogin: true})
    } else {
      console.log('密码错误')
      res.send({allowLogin: false})
    }
  })
})

router.get('/getArticleList', (req, res) => {
  let sql = "select id, title, text, img, time from article"
  pool.query(sql, (error, results, field) => {
    res.json(results)
  })
})

router.get('/articleDetail', (req, res) => {
  let sql = "select * from article where id = ?"
  let params = req.query.id
  pool.query(sql, params, (error, results, field) => {
    res.json(results)
  })
})

router.post('/addArticle', (req, res) => {
  let sql = "insert into article (title, text, img, time, detail, user) value (?, ?, ?, ?, ?, ?)"
  let params = [req.body.title, req.body.text, req.body.img, req.body.time, req.body.detail, req.body.user]
  pool.query(sql, params, (err, results, field) => {
    if (err) throw err
  })
})

router.post('/uploadArticleImg', (req, res) => {
  let sql = "update article set img = ? where id = 4"
  let params = req.body.imgData
  pool.query(sql, params, (err, results, field) => {

  })
})

router.get('/text', (req, res) => {
  pool.query("select img from article where id = 4", function(err, results, field) {
    res.json(results)
  })
})

module.exports = router;
